﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using IRUM.Core;
using IRUM.XingAPI;
using IRUM.Utils;

namespace IRUM.TR
{
    /// <summary>
    /// 시간대별투자자매매추이
    /// </summary>
    public class t1602 : CBaseQuery
    {
        public event InvestorTradeHandler OnReceived;
        private List<InvestorTrade> m_list = new List<InvestorTrade>();

        private string kind = string.Empty;
        private string lastDBDate = string.Empty;
        private string cts_time = " ";

        /// <summary>
        /// 시간대별투자자매매추이
        /// <param name="kind">시장구분, K:코스피, D:코스닥, F:선물, C:콜옵션, P:풋옵션</param>
        /// <param name="lastDBDate">DB에 저장된 마지막 날짜</param>
        /// </summary>
        public t1602(string kind, string lastDBDate)
        {
            this.kind = kind;
            this.lastDBDate = lastDBDate;

            SetClassData(TR_Type.t1602);
        }

        public void SetOutBlockData(int Index)
        {
            InvestorTrade it = new InvestorTrade();
            it.date = IRUM_Utils.GetLastTradingDateForDay(DateTime.Now).ToString("yyyyMMdd");    //마지막 거래일 가져와서 처리해야 함.
            it.gubun = "M";
            it.time = m_Query.GetFieldData(m_OutBlock1Name, "time", Index);
            it.native_sum = Convert.ToInt64(m_Query.GetFieldData(m_OutBlock1Name, "sv_08", Index));
            it.foreign_sum = Convert.ToInt64(m_Query.GetFieldData(m_OutBlock1Name, "sv_17", Index));
            it.organization_sum = Convert.ToInt64(m_Query.GetFieldData(m_OutBlock1Name, "sv_18", Index));

            m_list.Add(it);
        }

        public override void HandleQuery()
        {
            int OccursCount = m_Query.GetBlockCount(m_OutBlock1Name);

            for (int OccursIndex = 0; OccursIndex < OccursCount; OccursIndex++)
            {
                try
                {
                    SetOutBlockData(OccursIndex);
                }
                catch (Exception e)
                {
                    Debug.Print(e.ToString());
                    continue;
                }
            }

            //연속조회
            if (m_Query.IsNext == true)
            {
                cts_time = m_Query.GetFieldData(m_OutBlockName, "cts_time", 0);
                RequestNext();
            }
            else
            {
                // 이벤트 발생
                if (OnReceived != null)
                {
                    OnReceived(this, new InvestorTradeArgs(m_list));
                }
            }
        }

        protected override void SetRequestData()
        {
            string market = string.Empty;
            string upcode = string.Empty;

            switch (kind)
            {
                case "K":           //코스피
                    market = "1";
                    upcode = "001";
                    break;
                case "D":           //코스닥
                    market = "3";
                    upcode = "301";
                    break;
                case "F":           //선물
                    market = "4";
                    upcode = "900";
                    break;
                case "C":
                    market = "5";   //콜옵션
                    upcode = "700";
                    break;
                case "P":
                    market = "6";   //풋옵션
                    upcode = "800";
                    break;
            }

            m_Query.SetFieldData(m_InBlockName, "market", 0, market);                               //시장구분, 1, 1:코스피, 2:코스피200, 3:코스닥, 4:선물, 5:콜옵션, 6:풋옵션
            m_Query.SetFieldData(m_InBlockName, "upcode", 0, upcode);                                //업종코드, 1, market값(1,2)과 무관하게 이것으로 조회됨. 선물/옵션은 이 값과 관계없음
            m_Query.SetFieldData(m_InBlockName, "gubun1", 0, "2");          //수량구분, 1, 1:수량, 2:금액
            m_Query.SetFieldData(m_InBlockName, "gubun2", 0, "0");          //전일구분, 1, 0:금일, 1:전일
            m_Query.SetFieldData(m_InBlockName, "cts_time", 0, cts_time);   //연속조회용 시간, 처음은 space, 조회 후 outblock의 cts_time
            m_Query.SetFieldData(m_InBlockName, "cts_idx", 0, "");          //CTSIDX, 4, 사용하지 않음
            m_Query.SetFieldData(m_InBlockName, "cnt", 0, "500");           //조회건수, 4, 500이어야 함
            m_Query.SetFieldData(m_InBlockName, "gubun3", 0, "");           //직전대비구분, 1, C:직전대비
        }
    }
}
